//
//  CameraDataBaseTool.m
//  XiaoMiao
//
//  Created by Jim huang on 2018/5/23.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "CameraDataBaseTool.h"
#import "KV2CameraAngleNormalModel.h"

#import <FMDB.h>

static FMDatabase *_db;

@implementation CameraDataBaseTool

//+ (void)initialize{
//
//    _db = [FMDatabase databaseWithPath:DataBasePath];
//
//    if ([_db open]) {
//
//        NSLog(@"打开成功!");
//
//        BOOL success =[_db executeUpdate:@"create table if not exists t_cache (id integer primary key autoincrement,userID text,authkey text,dict blob not null);"];
//
//        if (success) {
//            NSLog(@"创表成功");
//        }else{
//            NSLog(@"创表失败");
//        }
//
//    }else{
//        NSLog(@"打开失败!");
//    }
//}

//初始化表
+ (void)initializeCameraAngleTable
{
    _db = [FMDatabase databaseWithPath:DataBasePath];

    if ([_db open]) {

        NSLog(@"打开成功!");

        //创建分级分类
        NSString * SQLCreateCateStr = @"create table if not exists CameraAngleCate  (id integer primary key ,cateViews BLOB,imageUrl text,name text,parentId text,tag text,dept text);";

        BOOL cateTableSuccess =[_db executeUpdate:SQLCreateCateStr];

    
        if (cateTableSuccess) {
            NSLog(@"创建cate表成功");
            //创建角度
            NSString * SQLCreateAngleStr = @"create table if not exists CameraAngleList  (id integer primary key ,cateId text,imageUrl text,name text,sort text,subCateId text,subSubCateId text,normalImageUrl text,syncImageUrl text,isOneself text);";
            BOOL AngleTableSuccess =[_db executeUpdate:SQLCreateAngleStr];
            
            if (AngleTableSuccess) {
                NSLog(@"创建angleCate表成功");

            }else{
                NSLog(@"创建angleCate表失败");

            }
        }else{
            NSLog(@"创建cate表失败");
        }

    }else{
        NSLog(@"打开失败!");
    }
}

//保存分级或角度数据
+ (void)saveAngleDataWithCateArr:(NSArray<KV2GetCameraAngleCateModel *> *)cateArr subCateArr:(NSArray<KV2GetCameraAngleCateModel *> *)subCateArr angleArr:(NSArray<KV2CameraAngleModel *> *)angleArr{
    
    if (cateArr.count>0) {
        for (KV2GetCameraAngleCateModel * model in cateArr) {
            //一级分类
            NSString * cateSQLStr = [NSString stringWithFormat:@"replace into CameraAngleCate (id,cateViews,imageUrl,name,parentId,tag,dept) values('%@','%@','%@','%@','%@','%@','%@')",model.id,model.cateViews,model.imageUrl,model.name,model.parentId,model.tag,@"1"];
            
            BOOL success =[_db executeUpdate:cateSQLStr];
            if (success) {
                NSLog(@"插入一级成功");
            }else{
                NSLog(@"插入一级失败");
            }
        }
    }
    
    
    if (subCateArr.count>0) {
        for (KV2GetCameraAngleCateModel * subCateModel in subCateArr) {
            //二级分类
            NSString * subCateSQLStr = [NSString stringWithFormat:@"replace into CameraAngleCate (id,cateViews,imageUrl,name,parentId,tag,dept) values('%@','%@','%@','%@','%@','%@','%@')",subCateModel.id,subCateModel.cateViews,subCateModel.imageUrl,subCateModel.name,subCateModel.parentId,subCateModel.tag,@"2"];
            
            BOOL success =[_db executeUpdate:subCateSQLStr];
            if (success) {
                NSLog(@"插入二级成功");
            }else{
                NSLog(@"插入二级失败");
            }
        }
    }
    
    
    if (angleArr.count>0) {
        
        for (KV2CameraAngleModel * angleModel in angleArr) {
            //创建角度分类
            NSString * angleSQLStr = [NSString stringWithFormat:@"replace into CameraAngleList (id,cateId,imageUrl,name,sort,subCateId,subSubCateId,normalImageUrl,syncImageUrl) values('%@','%@','%@','%@','%@','%@','%@','%@','%@')",angleModel.id,angleModel.cateId,angleModel.imageUrl,angleModel.name,angleModel.sort,angleModel.subCateId,angleModel.subSubCateId,angleModel.normalImageUrl,angleModel.syncImageUrl];
            
            BOOL success =[_db executeUpdate:angleSQLStr];
            if (success) {
                NSLog(@"插入角度成功");
            }else{
                NSLog(@"插入角度失败");
            }
        }
    }
}


//显示分级所有数据
+ (void)showAllCateList{
    FMResultSet *set;
    
    set = [_db executeQuery:@"select * from CameraAngleCate"];
    
    while ([set next]) {
        
        KV2GetCameraAngleCateModel * model = [[KV2GetCameraAngleCateModel alloc]init];
        
        model.id = [set objectForColumn:@"id"];
        model.cateViews = [set objectForColumn:@"cateViews"];
        model.imageUrl = [set objectForColumn:@"imageUrl"];
        model.name = [set objectForColumn:@"name"];
        model.parentId = [set objectForColumn:@"parentId"];
        model.tag = [set objectForColumn:@"tag"];
        model.dept = [set objectForColumn:@"dept"];
        NSLog(@"id %@----dept %@----parentId %@----name %@",model.id,model.dept,model.parentId,model.name);
    }
}

//获取分级数据
+ (NSArray *)getCateListWithDept:(NSString *)dept parentId:(NSString *)parentId{
    
    
    FMResultSet *set;
    
    if (parentId == nil) {
        set = [_db executeQuery:@"select * from CameraAngleCate where dept = ?  order by id asc",dept];
    }else{
        set = [_db executeQuery:@"select * from CameraAngleCate where dept = ? and parentId = ? order by id asc",dept,parentId];
    }

    
    NSMutableArray * listArr = [NSMutableArray array];
    
    while ([set next]) {
        
        KV2GetCameraAngleCateModel * model = [[KV2GetCameraAngleCateModel alloc]init];
        
        model.id = [set objectForColumn:@"id"];
        model.cateViews = [set objectForColumn:@"cateViews"];
        model.imageUrl = [set objectForColumn:@"imageUrl"];
        model.name = [set objectForColumn:@"name"];
        model.parentId = [set objectForColumn:@"parentId"];
        model.tag = [set objectForColumn:@"tag"];
        model.dept = [set objectForColumn:@"dept"];
        [listArr addObject:model];
    }
    
    return [NSArray arrayWithArray:listArr];
    
}


//显示所有角度数据
+(void)showAllAngle{
    
    FMResultSet *set;
    
    set = [_db executeQuery:@"select * from CameraAngleList"];
          
    while ([set next]) {
        
        KV2CameraAngleModel * model = [[KV2CameraAngleModel alloc]init];
        model.id = [set objectForColumn:@"id"];
        model.cateId = [set objectForColumn:@"cateId"];
        model.imageUrl = [set objectForColumn:@"imageUrl"];
        model.name = [set objectForColumn:@"name"];
        model.sort = [set objectForColumn:@"sort"];
        model.subCateId = [set objectForColumn:@"subCateId"];
        model.subSubCateId = [set objectForColumn:@"subSubCateId"];
        model.normalImageUrl = [set objectForColumn:@"normalImageUrl"];
        model.syncImageUrl = [set objectForColumn:@"syncImageUrl"];
        
        NSLog(@"id %@----cateId %@----subSubCateId %@----name %@",model.id,model.cateId,model.subSubCateId,model.name);
    }
           
}
           
//获取角度数据
+(NSArray *)getAngleWithSubCateId:(NSString *)subCateId {
    
    FMResultSet *set;
    
    set = [_db executeQuery:@"select * from CameraAngleList where  subSubCateId = ? order by id asc",subCateId];
    
    NSMutableArray * listArr = [NSMutableArray array];
    
    while ([set next]) {
        
        KV2CameraAngleModel * model = [[KV2CameraAngleModel alloc]init];
        model.id = [set objectForColumn:@"id"];
        model.cateId = [set objectForColumn:@"cateId"];
        model.imageUrl = [set objectForColumn:@"imageUrl"];
        model.name = [set objectForColumn:@"name"];
        model.sort = [set objectForColumn:@"sort"];
        model.subCateId = [set objectForColumn:@"subCateId"];
        model.subSubCateId = [set objectForColumn:@"subSubCateId"];
        model.normalImageUrl = [set objectForColumn:@"normalImageUrl"];
        model.syncImageUrl = [set objectForColumn:@"syncImageUrl"];

        [listArr addObject:model];
    }
    
    return [NSArray arrayWithArray:listArr];
    
}


@end
